

package com.lemon.cloud.generator.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lemon.cloud.generator.entity.GenFormConf;
import com.lemon.cloud.generator.service.GenFormConfService;
import com.lemon.cloud.comm.model.ResultMsg;
import com.lemon.cloud.log.annotation.SysLog;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 表单管理
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/form")
public class GenFormConfController {

    private final GenFormConfService genRecordService;

    /**
     * 分页查询
     *
     * @param page     分页对象
     * @param formConf 生成记录
     * @return
     */
    @GetMapping("/page")
    public ResultMsg<IPage<GenFormConf>> getGenFormConfPage(Page page, GenFormConf formConf) {
        return ResultMsg.resultSuccess(genRecordService.page(page, Wrappers.query(formConf)));
    }

    /**
     * 通过id查询生成记录
     *
     * @param id id
     * @return ResultMsg
     */
    @GetMapping("/{id}")
    public ResultMsg<GenFormConf> getById(@PathVariable("id") Integer id) {
        return ResultMsg.resultSuccess(genRecordService.getById(id));
    }

    /**
     * 通过id查询生成记录
     *
     * @param dsName    数据源ID
     * @param tableName tableName
     * @return ResultMsg
     */
    @GetMapping("/info")
    public ResultMsg<String> form(String dsName, String tableName) {
        return ResultMsg.resultSuccess(genRecordService.getForm(dsName, tableName));
    }

    /**
     * 新增生成记录
     *
     * @param formConf 生成记录
     * @return ResultMsg
     */
    @SysLog("新增生成记录")
    @PostMapping
    @PreAuthorize("hasAuthority('gen_form_add')")
    public ResultMsg<Boolean> save(@RequestBody GenFormConf formConf) {
        return ResultMsg.resultSuccess(genRecordService.save(formConf));
    }

    /**
     * 通过id删除生成记录
     *
     * @param id id
     * @return ResultMsg
     */
    @SysLog("通过id删除生成记录")
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('gen_form_del')")
    public ResultMsg<Boolean> removeById(@PathVariable("id") Long id) {
        return ResultMsg.resultSuccess(genRecordService.removeById(id));
    }

}
